<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of MessageAction
 *
 * @author QuangKhoi
 */
class MessageAction {
    public $messageActionType;
    public $messageId;
    public $threadId;
    public $userId;
    public $streamId;
    
    public function __construct() {
        
    }
    
    public static function getByMessageId($threadId, $streamId, $messageActionType = '') {
        $db = Database::getInstance();
        $user = User::getLogedInUser();
        $query = "SELECT * FROM messageactiondata WHERE threadId = $threadId AND streamid = $streamId AND userid IN (SELECT userid FROM users WHERE teamid = $user->teamId)";
        if ($messageActionType != '')
            $query .= " AND type = $messageActionType";
        $result = $db->query($query);
        if ($result->returnCode == 200)
            if ($result->dataTable->numOfRow > 0) {
                $re = array();
                foreach($result->dataTable->rows as $row) {
                    $item = new self();
                    $item->messageActionType = $row['type'];
                    $item->messageId = $row['messageid'];
                    $item->streamId = $row['streamid'];
                    $item->threadId = $row['threadid'];
                    $item->userId = $row['userid'];
                    $re[$row['type']] = $item;
                }
                return $re;
            }
        return null;
    }
    
    public static function getMessageIds($streamId, $messageType, $index, $length) {
        $db = Database::getInstance();
        $user = User::getLogedInUser();
        $query = "SELECT * FROM messageactiondata WHERE type = $messageType AND streamid = $streamId AND userid IN (SELECT userid FROM users WHERE teamid = $user->teamId) LIMIT $index, $length";
        $result = $db->query($query);
        if ($result->returnCode == 200)
            if ($result->dataTable->numOfRow > 0) {
                $re = array();
                $messageIds = array();
                $threadIds = array();
                foreach($result->dataTable->rows as $row) {
                    $messageIds[] = $row['messageid'];
                    $threadIds[] = $row['threadid'];
                }
                return $messageIds;
            }
        return null;
    }
}

?>
